// vite.config.js
import UnoCSS from 'unocss/vite'
import { UserConfig, defineConfig } from "vite";
import viteCompression from "vite-plugin-compression";
import vue from "@vitejs/plugin-vue";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite";
import { fileURLToPath } from "url";

import { resolve, dirname } from "path";

export default defineConfig(({ mode }): UserConfig => {
  console.log(mode);
  return {
    base: "",
    // root,
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"), //设置别名
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          // additionalData: `$injectedColor: orange;`,
          // additionalData: `@import '@/assets/styles/element-theme.scss';`, // 自定义主题，会减慢代码实时编译速度, 加上的话，把main.ts的 element-plus/dist/index.css 去掉
        },
      },
    },
    plugins: [
      vue(),
      UnoCSS(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [resolve(process.cwd(), "src/icons")],
        // Specify symbolId format
        symbolId: "icon-[dir]-[name]",
      }),
      // 代码压缩
      viteCompression({
        // deleteOriginFile: true, // 压缩后是否删除源文件
      }),
      // 去除I18n警告
      mode === "development"
        ? VueI18nPlugin({
            /* options */
            // locale messages resource pre-compile option
            include: resolve(
              dirname(fileURLToPath(import.meta.url)),
              "./path/to/src/locales/**"
            ),
          })
        : null,
    ],
    server: {
      host: "0.0.0.0",
      port: 9999,
      open: false,
      proxy: {
        "/api": {
          target: "http://localhost:3000/api/v1",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/img_url": {
          target: "http://localhost:3000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/img_url/, ""),
        },
      },
    },
    build: {
      minify: "terser",
      rollupOptions: {
        output: {
          manualChunks: {},
        },
      },
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: true,
        },
      },
      chunkSizeWarningLimit: 2000,
    },
  };
});
